 var utils = {  
  setParam : function (name,value){  
      localStorage.setItem(name,value)  
  },  
  getParam : function(name){  
      return localStorage.getItem(name)  
  }  
}  


var orderdetail={  
  username:"",  
  phone:"",  
  address:"",  
  zipcode:"",  
  totalNumber:0,  
  totalAmount:0.00      
}

var cart = {  
  //向购物车中添加商品 
  addproduct: function (product) { 
      var ShoppingCart = utils.getParam("ShoppingCart");  
      if (ShoppingCart == null || ShoppingCart == "") {  
          //第一次加入商品  
          var jsonstr = { "productlist": [{ "itemNo": product.itemNo, "catNo": product.catNo,"allPrice":product.num * product.price,"itemTitle": product.itemTitle, "narrowPicUrl": product.narrowPicUrl, "price": product.price, "num": product.num,checked: false}], "totalNumber": product.num, "totalAmount": (product.price * product.num) };  
          utils.setParam("ShoppingCart", "'" + JSON.stringify(jsonstr));  
      } else {  
          var jsonstr = JSON.parse(ShoppingCart.substr(1, ShoppingCart.length));  
          var productlist = jsonstr.productlist;  
          var result = false;  
          //查找购物车中是否有该商品  
          for (var i in productlist) {  
              if (productlist[i].itemNo == product.itemNo) {  
                  //购物车中的项目不能重复，所以num被这只为1
                  //productlist[i].num = 1;  
                  productlist[i].num = parseInt(productlist[i].num) + parseInt(product.num);
                  result = true;  
              }  
          }  
          if (!result) {  
              //没有该商品就直接加进去  
              productlist.push({ "itemNo": product.itemNo, "catNo": product.catNo,"itemTitle": product.itemTitle,"allPrice":product.num * product.price, "narrowPicUrl": product.narrowPicUrl, "price": product.price, "num": product.num,checked: false});  
          }  
          //重新计算总价  
          jsonstr.totalNumber = parseInt(jsonstr.totalNumber) + parseInt(product.num);  
          jsonstr.totalAmount = parseFloat(jsonstr.totalAmount) + (parseInt(product.num) * parseFloat(product.price));  
          orderdetail.totalNumber = jsonstr.totalNumber;  
          orderdetail.totalAmount = jsonstr.totalAmount;  
          //保存购物车  
          utils.setParam("ShoppingCart", "'" + JSON.stringify(jsonstr));  
      }  
  },  
  //修改购买商品数量  
  updateproductnum: function (itemNo, num) {  
      var ShoppingCart = utils.getParam("ShoppingCart");  
      var jsonstr = JSON.parse(ShoppingCart.substr(1, ShoppingCart.length));  
      var productlist = jsonstr.productlist;  

      for (var i in productlist) {  
          if (productlist[i].itemNo == itemNo) {  
              jsonstr.totalNumber = parseInt(jsonstr.totalNumber) + (parseInt(num) - parseInt(productlist[i].num));  
              jsonstr.totalAmount = parseFloat(jsonstr.totalAmount) + ((parseInt(num) * parseFloat(productlist[i].price)) - parseInt(productlist[i].num) * parseFloat(productlist[i].price));  
              productlist[i].num = parseInt(num);  
              orderdetail.totalNumber = jsonstr.totalNumber;  
              orderdetail.totalAmount = jsonstr.totalAmount;  
              utils.setParam("ShoppingCart", "'" + JSON.stringify(jsonstr));  
              return;  
          }  
      }  
  },  
  //获取购物车中的所有商品  
  getproductlist: function () {  
      var ShoppingCart = utils.getParam("ShoppingCart");  
      var jsonstr = JSON.parse(ShoppingCart.substr(1, ShoppingCart.length));  
      var productlist = jsonstr.productlist;  
      productlist.totalNumber = jsonstr.totalNumber;  
      productlist.totalAmount = jsonstr.totalAmount*100;  
      return productlist;  
  },  
  //判断购物车中是否存在商品  
  existproduct: function (itemNo) {  
      var result = false;  
      var ShoppingCart = utils.getParam("ShoppingCart");  
      if (ShoppingCart != null) {  
          var jsonstr = JSON.parse(ShoppingCart.substr(1, ShoppingCart.length));  
          var productlist = jsonstr.productlist;  
          for (var i in productlist) {  
              if (productlist[i].id == itemNo) {  
                  result = true;  
              }  
          }  
      }  
      return result;  
  },  
  //删除购物车中商品  
  deleteproduct: function (itemNo) {  
      var ShoppingCart = utils.getParam("ShoppingCart");  
      var jsonstr = JSON.parse(ShoppingCart.substr(1, ShoppingCart.length));  
      var productlist = jsonstr.productlist;  
      var list = [];  
      for (var i in productlist) {  
          if (productlist[i].itemNo == itemNo) {  
              jsonstr.totalNumber = parseInt(jsonstr.totalNumber) - parseInt(productlist[i].num);  
              jsonstr.totalAmount = parseFloat(jsonstr.totalAmount) - parseInt(productlist[i].num) * parseFloat(productlist[i].price);  
          } else {  
              list.push(productlist[i]);  
          }  
      }  
      jsonstr.productlist = list;  
      orderdetail.totalNumber = jsonstr.totalNumber;  
      orderdetail.totalAmount = jsonstr.totalAmount;  
      utils.setParam("ShoppingCart", "'" + JSON.stringify(jsonstr));  
  }  ,
  //修改购物车的选择状态
  changeSelect:function(itemNo, checked){
    var ShoppingCart = utils.getParam("ShoppingCart");  
    var jsonstr = JSON.parse(ShoppingCart.substr(1, ShoppingCart.length));  
    var productlist = jsonstr.productlist;  
    for (var i in productlist) {  
        if (productlist[i].itemNo == itemNo) {  
            productlist[i].checked = checked
            utils.setParam("ShoppingCart", "'" + JSON.stringify(jsonstr));  
            return;  
        }  
    }  
  }
}

export default cart